/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package myconfession.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.NamingException;
import javax.xml.bind.JAXBException;
import myconfession.dto.account.Account;
import myconfession.dto.account.Accounts;
import myconfession.utils.ConstantProvider;
import myconfession.utils.SQLConnection;
import myconfession.utils.XMLUtility;

/**
 *
 * @author Liem
 */
public class AccountDAO {

    public static Account checkLogin(String username, String password) {
        Connection con = null;
        PreparedStatement stm = null;
        ResultSet rs = null;
        Account acc = null;
        try {
            con = SQLConnection.getConnection();
            stm = con.prepareStatement("exec sp_checkLogin ?,?");
            stm.setString(1, username);
            stm.setString(2, password);
            rs = stm.executeQuery();
            if (rs.next()) {
                acc = new Account();
                acc.setUsername(rs.getString("username"));
                acc.setPassword(rs.getString("password"));
                acc.setEmail(rs.getString("email"));
                acc.setFirstname(rs.getString("firstname"));
                acc.setLastname(rs.getString("lastname"));
                acc.setFbToken(rs.getString("fbToken"));
            }
        } catch (NamingException ex) {
            Logger.getLogger(AccountDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(AccountDAO.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
                if (stm != null) {
                    stm.close();
                }
                if (con != null) {
                    con.close();
                }
            } catch (SQLException ex) {
                Logger.getLogger(AccountDAO.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

        return acc;
    }

    // BaoLA
    public static boolean createAccount(Account acc) {

        Connection con = null;
        PreparedStatement stm = null;
        // Account acc = null;
        try {
            con = SQLConnection.getConnection();
            stm = con.prepareStatement("exec sp_addAccount ?,?,?,?,?");
            stm.setString(1, acc.getUsername());
            stm.setString(2, acc.getPassword());
            stm.setString(3, acc.getEmail());
            stm.setString(4, acc.getFirstname());
            stm.setString(5, acc.getLastname());
            int rows = stm.executeUpdate();
            if (rows > 0) {
                return true;
            } else {
                return false;
            }
        } catch (NamingException ex) {
            Logger.getLogger(AccountDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(AccountDAO.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                if (stm != null) {
                    stm.close();
                }
                if (con != null) {
                    con.close();
                }
            } catch (SQLException ex) {
                Logger.getLogger(AccountDAO.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return false;

    }

    public static Accounts getAllAccount() {
        Connection con = null;
        PreparedStatement stm = null;
        ResultSet rs = null;
        Account acc = null;
        Accounts list = null;
        try {
            list = new Accounts();
            con = SQLConnection.getConnection();
            stm = con.prepareStatement("select * from Accounts");
            rs = stm.executeQuery();
            while (rs.next()) {
                acc = new Account();
                acc.setUsername(rs.getString("username"));
                acc.setPassword(rs.getString("password"));
                acc.setEmail(rs.getString("email"));
                acc.setFirstname(rs.getString("firstname"));
                acc.setLastname(rs.getString("lastname"));
                acc.setFbToken(rs.getString("fbToken"));
                list.getAccount().add(acc);
            }
            return list;
        } catch (NamingException ex) {
            Logger.getLogger(AccountDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(AccountDAO.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
                if (stm != null) {
                    stm.close();
                }
                if (con != null) {
                    con.close();
                }
            } catch (SQLException ex) {
                Logger.getLogger(AccountDAO.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return list;
    }

    public static boolean setFbToken(String username, String token) {
        Connection con = null;
        PreparedStatement stm = null;
        try {
            con = SQLConnection.getConnection();
            stm = con.prepareStatement("update Accounts set fbToken = ? where username = ?");
            stm.setString(1, token);
            stm.setString(2, username);
            int rows = stm.executeUpdate();
            if (rows > 0) {
                return true;
            }
        } catch (NamingException ex) {
            Logger.getLogger(AccountDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(AccountDAO.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                if (stm != null) {
                    stm.close();
                }
                if (con != null) {
                    con.close();
                }
            } catch (SQLException ex) {
                Logger.getLogger(AccountDAO.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return false;

    }

    public static boolean updateAccount(Account acc) {
        Connection con = null;
        PreparedStatement pr = null;
        try {
            con = SQLConnection.getConnection();
            pr = con.prepareStatement("exec sp_updateAccount ?,?,?,?");
            pr.setString(2, acc.getFirstname());
            pr.setString(3, acc.getLastname());
            pr.setString(4, acc.getEmail());
            pr.setString(1, acc.getUsername());
            int row = pr.executeUpdate();
            if (row > 0) {
                return true;
            }
        } catch (NamingException ex) {
            Logger.getLogger(AccountDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(AccountDAO.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                if (pr != null) {
                    pr.close();
                }
                if (con != null) {
                    con.close();
                }
            } catch (SQLException ex) {
                Logger.getLogger(AccountDAO.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return false;
    }
        public static boolean checkExistUser(String username) throws JAXBException{
        Accounts accs = (Accounts) XMLUtility.unmarshallJAXB(ConstantProvider.getInstance().getAccountXmlPath(), Accounts.class);
        for (Account tmp : accs.getAccount()) {
            if (tmp.getUsername().trim().equalsIgnoreCase(username)) {
                return true;
            }
        }
        return false;
    }
}
